package com.csdj.x315springboot.util;

import com.csdj.x315springboot.common.CacheConstant;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求相关的工具类
 */
public class RequestUtil {

    public static HttpServletRequest getRequest(){
        RequestAttributes requestAttributes =
                RequestContextHolder.currentRequestAttributes();
//RequestContextHolder.getRequestAttributes();
//从session里面获取对应的值
        HttpServletRequest request =
                ((ServletRequestAttributes)requestAttributes).getRequest();
        return request;
    }

    /**
     * 从请求头中取uid
     * @param request
     * @return
     */
    public static Integer getUserId(HttpServletRequest request){
        String uid =  request.getHeader(CacheConstant.HEADER_UID);
        if(StringUtils.hasText(uid)){
            return Integer.parseInt(uid);
        }
        return null;
    }
}
